# -*- coding:utf-8 -*-
import Pluto,struct,codecs,fnmatch,re

def makestr(i):
        string=[]
        j=0
        while j>=0:
                if re.match('//end',lines[i+1+j]):
                        break
                string.append(lines[i+1+j])
                j+=1
        return ''.join(string)[:-2]

fl=Pluto.walk('UI')
for fn in fl:
        if not fnmatch.fnmatch(fn,'*.txt'):
                continue
        print fn
        fp=codecs.open(fn,'rb','utf16')
        lines=fp.readlines()
        fp.close()
        line_num=len(lines)

        tar=open(fn[:-4],'wb')
        text_list=[]
        for i in xrange(line_num):
                if re.match('//start',lines[i]):
                        text=makestr(i)
                        text_list.append(text)
        tar.write('\x08\x00\x00\x00')
        num=len(text_list)
        tar.write(struct.pack('i',num))
        for text in text_list:
                text=text.replace('\r','')
                tar.write(struct.pack('i',len(text)*2+6))
                tar.write(text.encode('utf16')[2:])
                tar.write('\x00\x00')
        tar.close()
